package com.bqtt.tumor.demo.presentation.controller;

import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;

import com.bqtt.tumor.demo.business.BodyPartService;
import com.bqtt.tumor.demo.business.DeseaseService;
import com.bqtt.tumor.demo.business.DeseaseService;
import com.bqtt.tumor.demo.common.utility.InlineTools;
import com.bqtt.tumor.demo.entity.Desease;
import com.bqtt.tumor.demo.entity.Desease;
import com.bqtt.tumor.demo.entity.SystemUser;

public class desease {

	public static String queryDesease(SystemUser user, ModelMap model,
			Desease condtion, BindingResult bindingResult) {
		// TODO Auto-generated method stub

		if(user.getSessionState() ==0)
		{// 用户未登入
			if(!model.containsAttribute("user"))
				model.addAttribute("user", new SystemUser());
			return "login";
		}
		
		if(condtion==null)
		{
			condtion = new Desease();
		}
		
		if(!model.containsAttribute("new_desease"))
		     model.addAttribute("new_desease", new Desease(1));		
		if(!model.containsAttribute("edit_desease"))
		     model.addAttribute("edit_desease", new Desease());	

		
		model.addAttribute("historySearchCondition", condtion);
		
		model.addAttribute("body_part_list", BodyPartService.getList());
		
		model.addAttribute("condition", condtion); //保留上次查找的字段
		
	    model.addAttribute("desease_list", DeseaseService.searchByCondition(condtion, 20, 1));
		
	    model.addAttribute("isAddNewShow","false");
		
		return "admin/desease";
	}

	public static String addDesease(SystemUser user, ModelMap model,
			Desease new_desease, BindingResult bindingResult) {
		// TODO Auto-generated method stub
		//session 变量 ——不用自动添加到Model
				//condition currentUser
		model.addAttribute("body_part_list", BodyPartService.getList());
				Desease historySearchCondition = (Desease) model.get("historySearchCondition");
				if(historySearchCondition==null)
					historySearchCondition = new Desease();
				
				if(!model.containsAttribute("new_desease"))
				     model.addAttribute("new_desease", new Desease(1));		
				if(!model.containsAttribute("edit_desease"))
				     model.addAttribute("edit_desease", new Desease());
				
				if(bindingResult!=null && bindingResult.hasErrors())
				{
					//出错的话，就保持弹出窗口，显示错误
					model.addAttribute("isAddNewShow","true");
					
					// 其他变量需要手动添加到Model
					model.addAttribute("desease_list", DeseaseService.searchByCondition(historySearchCondition, 20, 1));
					
					model.addAttribute("condition", historySearchCondition);
					
					return "admin/desease";
				}
				
				if(user.getSessionState() ==0)
				{// 用户未登入
					if(!model.containsAttribute("currentUser"))
					{
						model.addAttribute("currentUser", new SystemUser());
					}
					return "login";
				}
				

				boolean result = DeseaseService.add(new_desease);
				
				if(result)
				{
					model.addAttribute("new_desease", new Desease());		
					model.addAttribute("information", "<font style='color:#5599FF'>操作成功！</font>");
				}
				else
				{
					model.addAttribute("information", "<font style='color:#FF3333'>操作失败！</font>");
				}
				
				// 其他变量需要手动添加到Model
				model.addAttribute("condition", historySearchCondition);
				model.addAttribute("desease_list", DeseaseService.searchByCondition(historySearchCondition, 20, 1));
			    model.addAttribute("isAddNewShow","false");

				return "admin/desease";
	}

	public static String editDesease(SystemUser user, ModelMap model,
			Desease edit_desease, BindingResult bindingResult) {
		model.addAttribute("body_part_list", BodyPartService.getList());
		Desease historySearchCondition = (Desease) model.get("historySearchCondition");
		if(historySearchCondition==null)
			historySearchCondition = new Desease();
		
		if(!model.containsAttribute("new_desease"))
		     model.addAttribute("new_desease", new Desease(1));		
		if(!model.containsAttribute("edit_desease"))
		     model.addAttribute("edit_desease", new Desease());
		
		if(bindingResult!=null && bindingResult.hasErrors())
		{
			//出错的话，就保持弹出窗口，显示错误
			
			model.addAttribute("isAddNewShow","true");
			// 其他变量需要手动添加到Model
			model.addAttribute("desease_list", DeseaseService.searchByCondition(historySearchCondition, 20, 1,edit_desease.getDesease_def_id()));
			
			model.addAttribute("condition", historySearchCondition);
			
			return "admin/desease";
		}
		
		if(user.getSessionState() ==0)
		{// 用户未登入
			if(!model.containsAttribute("currentUser"))
			{
				model.addAttribute("currentUser", new SystemUser());
			}
			return "login";
		}
		

		boolean result = DeseaseService.update(edit_desease);
		
		if(result)
		{
			model.addAttribute("new_desease", new Desease());		
			model.addAttribute("information", "<font style='color:#5599FF'>操作成功！</font>");
		}
		else
		{
			model.addAttribute("information", "<font style='color:#FF3333'>操作失败！</font>");
		}
		
		// 其他变量需要手动添加到Model
		model.addAttribute("condition", historySearchCondition);
		model.addAttribute("desease_list", DeseaseService.searchByCondition(historySearchCondition, 20, 1));
	    model.addAttribute("isAddNewShow","false");

		return "admin/desease";
	}

}
